/**
 * vuex的actions模块
 */
import {RECEIVE_ADDRESS,RECEIVE_CATEGORYS,RECEVIE_SHOPS,RECEVIE_USER_INFO,RESET_USER_INFO,RECEIVE_GOODS,RECEIVE_INFO,RECEIVE_RATINGS,INCREMENT_FOOD_COUNT,DECREMENT_FOOD_COUNT,CLEAR_CART, RECEIVE_SEARCH_SHOPS} from './mutation-types.js';
import {reqAddress,reqCategorys,reqShops,reqUser,reqLogout,reqShopGoods,reqShopRatings,reqShopInfo, reqSearchShops} from '../api/index';

export default{ 
    //异步获取地址
    async getAddress({commit,state}){
        //发送异步的ajax请求
        const geohash = state.latitude+","+state.longitude;
        const result = await reqAddress(geohash);
        //提交一个mutations
        if(result.code===0){
            let address = result.data;
            commit(RECEIVE_ADDRESS,{address});            
        }
    },
    //异步获取食品分类列表
    async getCategorys({commit,state}){
        //发送异步的ajax请求
        const result = await reqCategorys();
        //提交一个mutations
        if(result.code ==0 ){
            let categorys = result.data;
            commit(RECEIVE_CATEGORYS,{categorys});
        }
    },
    //异步获取商家列表
    async getShops({commit,state}){
         const {latitude,longitude} = state;
        //发送异步的ajax请求
        const result = await reqShops({latitude,longitude});
        if(result.code ==0 ){
            const shops = result.data;
            commit(RECEVIE_SHOPS,{shops});
        }
    },
    //同步记录用户信息
    recordUser({commit},userInfo){
        commit(RECEVIE_USER_INFO,{userInfo});
    },
    //异步获取用户信息
    async getUserInfo({commit}){
        const result = await reqUser();
        if(result.code===0){
            const userInfo = result.data;
            commit(RECEVIE_USER_INFO,{userInfo});
        }
    },
    //异步登出
    async logout({commit}){
        const result = await reqLogout();
        if(result.code===0){
            commit(RESET_USER_INFO);
        }
    },
    //异步获取商家信息
    async getShopInfo({commit},cb){
        const result = await reqShopInfo();
        if(result.code===0){
            const info = result.data;
            info.score = 3.5;
            commit(RECEIVE_INFO,{info});
            cb && cb();
        }
    },
    //异步获取商家评论列表
    async getShopRatings({commit},callback){
        const result = await reqShopRatings();
        if(result.code ===0){
            const ratings = result.data;
            commit(RECEIVE_RATINGS,{ratings});
            callback && callback();
        }
    },
    //异步获取商家商品列表
    async getShopGoods({commit},cb){
        const result = await reqShopGoods();
        if(result.code === 0){ 
            const goods = result.data;
            commit(RECEIVE_GOODS,{goods});
            //通知组件数据加载完成
            cb && cb();
        }
    },
    //同步更新food中的count值
    updateFoodCount({commit},{isAdd,food}){
        if(isAdd){
            commit(INCREMENT_FOOD_COUNT,{food});
        }else{
            commit(DECREMENT_FOOD_COUNT,{food});
        }
    },
    //同步清空购物车
    clearCart({commit}){
        commit(CLEAR_CART);
    },
    //异步获取所有的商家数组
    async searchShops({commit,state},keyword){
        const geohash = state.latitude+","+state.longitude;
        const result = await reqSearchShops({geohash,keyword});
        if(result.code === 0){
            const searchShops = result.data;
            commit(RECEIVE_SEARCH_SHOPS,{searchShops});
        }
    }
}